<%@include file="/pub/import.jsp"%>
<%@include file="/pub/header.jsp"%>
<%
	int projectID = RequestUtil
			.getIntParameter(request, "projectId", 0);
	Project project = ProjectHandler.getProject(projectID);
	int treeId = RequestUtil.getIntParameter(request, "treeId", 0);

	List<User> userList = new ArrayList<User>();

    
    int[] pidarr = new int[]{0,0,0,0};
    if(!StringUtils.isEmpty(project.getBuildUnit())) pidarr[0] = 1;
    if(!StringUtils.isEmpty(project.getConstructUnit())) pidarr[1] = 3;
    if(!StringUtils.isEmpty(project.getSupervisorUnit())) pidarr[2] = 40;
    if(!StringUtils.isEmpty(project.getDesignUnit())) pidarr[3] = 44;
    List<ScoreTree> treeList = ScoreTreeHandler.loadByPIDs(pidarr);    
    
    String users = project.getScoreUser();
    if(!StringUtils.isEmpty(users)) {
      String[] userArr = users.split(",");
      for(String userId : userArr) {
        userList.add(UserHandler.getUser(Integer.parseInt(userId)));
      }
    }

	/*int[] pidarr = new int[] { 0, 0, 0, 0 };
	if (!StringUtils.isEmpty(project.getBuildUnit()))
		pidarr[0] = 1;
	if (!StringUtils.isEmpty(project.getConstructUnit()))
		pidarr[1] = 3;
	if (!StringUtils.isEmpty(project.getDesignUnit()))
		pidarr[2] = 40;
	if (!StringUtils.isEmpty(project.getSupervisorUnit()))
		pidarr[3] = 44;
	List<ScoreTree> treeList = ScoreTreeHandler.loadByPIDs(pidarr);

	String users = project.getScoreUser();
	if (!StringUtils.isEmpty(users)) {
		String[] userArr = users.split(",");
		for (String userId : userArr) {
			userList.add(UserHandler.getUser(Integer.parseInt(userId)));
		}
	}*/

	float totalScore = 0.0f;
	if (!StringUtils.isEmpty(project.getBuildUnit()))
		totalScore += 100;
	if (!StringUtils.isEmpty(project.getConstructUnit()))
		totalScore += 100;
	if (!StringUtils.isEmpty(project.getDesignUnit()))
		totalScore += 100;
	if (!StringUtils.isEmpty(project.getSupervisorUnit()))
		totalScore += 100;

	double result = 0.0d;
	result = ProjectItemsHandler.getProjectIdScoreSum(projectID);
	BigDecimal b = new BigDecimal(result);
	double currentScore = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

	ScoreTree tree = ScoreTreeHandler.loadById(treeId);//延时加载评分子项,先加载树
	//得到当前tree的ScoreItem对像
	/*
	Set<ScoreItem> scoreItem = tree.getScoreItems();//得到最终评分子项,最终评分子项关联评分项表中的ProjectItems
	Map<Integer, ScoreItem>  scorteItemMap = new HashMap<Integer, ScoreItem>();
	if(scoreItem!=null)
	{
    float totalScore = 0.0f;
    if(!StringUtils.isEmpty(project.getBuildUnit())) totalScore+=100;
    if(!StringUtils.isEmpty(project.getConstructUnit())) totalScore+=100;
    if(!StringUtils.isEmpty(project.getSupervisorUnit())) totalScore+=100;
    if(!StringUtils.isEmpty(project.getDesignUnit())) totalScore+=100;
    
    float currentScore = 0.0f;
    currentScore = ProjectItemsHandler.getProjectIdScoreSum(projectID);
    
    ScoreTree tree = ScoreTreeHandler.loadById(treeId);//延时加载评分子项,先加载树
    //得到当前tree的ScoreItem对像
    /*
    Set<ScoreItem> scoreItem = tree.getScoreItems();//得到最终评分子项,最终评分子项关联评分项表中的ProjectItems
    Map<Integer, ScoreItem>  scorteItemMap = new HashMap<Integer, ScoreItem>();
    if(scoreItem!=null)
    {
	    for(ScoreItem list_scoreItem:scoreItem)
	    {
		    if(!scorteItemMap.containsKey(list_scoreItem.getId()))
		    {
		    	scorteItemMap.put(list_scoreItem.getId(),list_scoreItem);
		    }
	    }
	}
	
	//取得项目中的所有ProjectItems,再去取关联的ScoreItem对像
	Set<ProjectItems> projectItem = project.getProjectItemses();
	Map<Integer, Set<ProjectItems>>  projectItemMap = new HashMap<Integer, Set<ProjectItems>>();
	if(projectItem!=null)
	{
		for(ProjectItems pi :projectItem)
		{
	    	if(!projectItemMap.containsKey(pi.getScoreItem().getId()))
	    	{
	    		projectItemMap.put(pi.getScoreItem().getId(),pi.getScoreItem().getProjectItemses());
	    	}
	    	currentScore+=pi.getScore();
		}
	}
	
	Set<ProjectItems> resultProjectItems= new HashSet<ProjectItems>();
	Iterator it = scorteItemMap.keySet().iterator();
	while(it.hasNext())
	{
		Integer key = (Integer)it.next();
		if(projectItemMap.containsKey(key))
		{
			resultProjectItems.addAll(projectItemMap.get(key));
		}
	}*/

	List<ProjectItems> resultProjectItems = ProjectItemsHandler
			.getByProjectIdTreeId(projectID, treeId);
	List<ProjectItems> allProjectItems = ProjectItemsHandler
			.getByProject(projectID);
	Map<Integer, String> allProjectItemMap = new HashMap<Integer, String>();
	for (ProjectItems p : allProjectItems) {
		allProjectItemMap.put(p.getScoreItem().getScoreTree().getId(),
				"true");
	}
	context.put("allProjectItemMap", allProjectItemMap);
	context.put("totalScore", totalScore);
	context.put("currentScore", currentScore);
	context.put("treeList", treeList);
	context.put("userList", userList);
	context.put("project", project);
	context.put("DateUtil", new DateUtil());

	context.put("scoreTree", tree);
	context.put("projectItemsList", resultProjectItems);
	out.println(TemplateUtil.getOutput(context,
			"/view/project/ProjectView.html"));
%>
<%@include file="/pub/footer.jsp"%>